package com.guigu.www.denghao.controller;

import com.guigu.www.denghao.service.DHChatTableService;
import com.guigu.www.pojo.ChatTable;
import com.guigu.www.utils.FtpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/DHChatTableController")
public class DHChatTableController {

    @Autowired
    private DHChatTableService chatTableService;

    @PostMapping("/initChatTableList")
    public List<ChatTable> initChatTableList(Integer hmId, Integer chatTaState){
        return chatTableService.initChatTableList(hmId,chatTaState);
    }

    @PostMapping("/homeChatTableList")
    public List<ChatTable> homeChatTableList(Integer hmId){
        return chatTableService.homeChatTableList(hmId);
    }

    @PutMapping("/upConsultHmWatch")
    public Integer upConsultHmWatch(Integer chatTaId){
        return chatTableService.upConsultHmWatch(chatTaId);
    }

    @PostMapping("/addConsultMsg")
    public Integer addConsultMsg(Integer chatTaId,Integer conMesType,String conMesContent,Integer conMesState,String conMesAccount){
        return chatTableService.addConsultMsg(chatTaId,conMesType,conMesContent,conMesState,conMesAccount);
    }

    @PostMapping("/fileUploadImage")
    public Boolean fileUploadImage (@RequestParam("imgFile") MultipartFile imgFile,Integer chatTaId,Integer conMesType,Integer conMesState,String conMesAccount) throws IOException {
        if(imgFile.isEmpty()){
            return false;
        }else{
            // 获取上传的文件流
            InputStream inputStream = imgFile.getInputStream();

            String originalFilename = imgFile.getOriginalFilename();

            //获取源文件前缀
            String fileNamePrefix = originalFilename.substring(0,originalFilename.lastIndexOf("."));
            //获取源文件后缀
            String fileNameSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            // 将源文件前缀之后加上时间戳避免重名
            String newFileNamePrefix = fileNamePrefix + new Date().getTime();
            //得到上传后新文件的文件名
            String newFileName = newFileNamePrefix+fileNameSuffix;

            // 进行上传
            boolean b = FtpUtil.uploadFile(newFileName, inputStream);

            // 添加到数据库
            chatTableService.addConsultMsg(chatTaId,conMesType,newFileName,conMesState,conMesAccount);
        }
        return true;
    }

    @PostMapping("/fileUploadField")
    public Boolean fileUploadField (@RequestParam("fieldFile") MultipartFile fieldFile,Integer chatTaId,Integer conMesType,Integer conMesState,String conMesAccount) throws IOException {
        if(fieldFile.isEmpty()){
            return false;
        }else{
            // 获取上传的文件流
            InputStream inputStream = fieldFile.getInputStream();

            String originalFilename = fieldFile.getOriginalFilename();

            //获取源文件前缀
            String fileNamePrefix = originalFilename.substring(0,originalFilename.lastIndexOf("."));
            //获取源文件后缀
            String fileNameSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            // 将源文件前缀之后加上时间戳避免重名
            String newFileNamePrefix = fileNamePrefix + new Date().getTime();
            //得到上传后新文件的文件名
            String newFileName = newFileNamePrefix+fileNameSuffix;

            // 进行上传
            boolean b = FtpUtil.uploadFile(newFileName, inputStream);

            // 添加到数据库
            chatTableService.addConsultMsg(chatTaId,conMesType,newFileName,conMesState,conMesAccount);
        }
        return true;
    }

    // 修改会员列表状态的方法
    @PutMapping("/delChatList")
    public Integer delChatList (Integer chatTaId){
        return chatTableService.delChatList(chatTaId);
    }

    // 查看是否已经在咨询列表中
    @PostMapping("/queryByHmIdAndMemId")
    public ChatTable queryByHmIdAndMemId(Integer hmId,Integer memId){
        return chatTableService.queryByHmIdAndMemId(hmId,memId);
    }

    // 添加到聊天列表
    @PostMapping("/addChatList")
    public Integer addChatList(@RequestBody ChatTable chatTable){
        return chatTableService.addChatList(chatTable);
    }
}
